import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;


public class Compiler {
	
	private static ArrayList<String> imports = new ArrayList<String>();
	
	public static void main(String args[]) throws Exception{
		
		File out = new File(args[1]);
		
		try {
			System.out.println("Merging java files...");
			String output = mergeFolder((new File(args[0])).listFiles());
			PrintWriter writer = new PrintWriter(out);
			for(String imp : imports)
				writer.println(imp);
			writer.println(output);
			writer.flush();
			writer.close();
			
			
			writer = new PrintWriter(out.getParent()+File.separator+"MANIFEST.MF");
			writer.println("Main-Class: Dvonn4ever");
			writer.flush();
			writer.close();
			
			System.out.println("Compiling...");
			Process p = Runtime.getRuntime().exec("javac "+out);
			BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line;
			while((line = reader.readLine())!=null){
				System.out.println(line);
			}
			reader.close();
			Thread.sleep(1000);
			System.out.println("Creating jar...");
			String run = "jar -cmf "+out.getParent()+File.separator+"MANIFEST.MF "+args[2];
			File[] outdir = (new File(out.getParent())).listFiles();
			
			for(File f : outdir){
				if(f.getName().endsWith(".class")){
					run += " -C "+out.getParent()+" "+f.getName();
				}
			}
			
			p = Runtime.getRuntime().exec(run);
			reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
			while((line = reader.readLine())!=null){
				System.out.println(line);
			}
			reader.close();
			Thread.sleep(1000);
			
			System.out.println("Delete tmp files...");
			for(File f : outdir){
				if(f.getName().endsWith(".class") || f.getName().endsWith(".MF")){
					f.setWritable(true);
					f.delete();
				}
			}
			
			System.out.println("Source ready at : "+args[1]+"\njar ready at : "+args[2]);
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String mergeFolder(File[] dir) throws IOException{
		String output = "";
		for(File f : dir){
			if(f.isFile() && f.getName().endsWith(".java") && !f.getName().equals(Compiler.class.getName()+".java")){
				output+=addFile(f);
			}
		}
		
		for(File f : dir){
			if(f.isDirectory()){
				output+=mergeFolder(f.listFiles());
			}
		}
		return output;
	}
	
	public static String addFile(File file) throws IOException{
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line;
		String output = "";
		while((line = reader.readLine())!= null){
			if(line.startsWith("package")){
				
			} else if(line.startsWith("import")){
				if(line.contains("java") && imports.indexOf(line)==-1){
					imports.add(line);
				}
			} else if(line.contains("class") || line.contains("interface")){
				if(line.contains("Dvonn4ever")){
					output += line+"\n";
				} else {
					output += line.replaceAll("public", "")+"\n";
				}
			} else {
				output += line+"\n";
			}
		}
		reader.close();
		return output;
	}
}
